<?php
class Shop_Model_Category extends Promotor_Model_Scaffold {

	const UPLOAD_PATH = 'upload/shop/category/';
	
	protected $_dbTableClass = 'Shop_Model_DbTable_Category';
	
	protected $_cachedMethods = array(
		'findByAlias',
		'findById',
		'findAll',
		'findOne',
		'findChildrens',
		'findParents'
	);
	
	public function selectList() {
		return $this->getDbTable();
	}
	
	/**
	 * @param string|integer|KontorX_Db_Table_Tree_Row_Abstract $row
	 * @return KontorX_Db_Table_Tree_Rowset_Abstract
	 */
	public function findChildrens($row, $depthLevel = null) {
		$attachment = array();

		if (is_int($row)) {
			$row = $this->findById($row, false);
		} else
		if (is_string($row)) {
			$row = $this->findByAlias($row, false);
		}

		if (!$row instanceof KontorX_Db_Table_Tree_Row_Abstract) {
			$this->_setStatus(self::FAILURE);
			return;
		}
		
		$table = $this->getDbTable();
		$select = $table->select()
			->where('publicated = ?', 1);
		
		/* @var $row KontorX_Db_Table_Tree_Row_Abstract */
		$rowset = $row->findChildrens($depthLevel, $select);
		return $rowset;
	}
	
	/**
	 * @param string|integer|KontorX_Db_Table_Tree_Row_Abstract $row
	 * @return KontorX_Db_Table_Tree_Rowset_Abstract
	 */
	public function findParents($row, $depthLevel = null) {
		$attachment = array();

		if (is_int($row)) {
			$row = $this->findById($row, false);
		} else
		if (is_string($row)) {
			$row = $this->findByAlias($row, false);
		}

		if (!$row instanceof KontorX_Db_Table_Tree_Row_Abstract) {
			$this->_setStatus(self::FAILURE);
			return;
		}
		
		$table = $this->getDbTable();
		$select = $table->select()
			->where('publicated = ?', 1);
		
		/* @var $row KontorX_Db_Table_Tree_Row_Abstract */
		$rowset = $row->findParents($depthLevel, $select);
		return $rowset;
	}

	/**
	 * @param bool $publicated
	 * @return Zend_Navigation_Container
	 */
	public function getNavigation($publicated = true) {
		if ($publicated) {
			/* @var $rowset KontorX_Db_Table_Tree_Rowset_Abstract */
			$rowset = $this->findAllCache(null, false);
			// ratuje przed fatal error
			$rowset->setTable($this->getDbTable());
		} else {
			$rowset = $this->getDbTable()->fetchAll();
		}

		$navigation = new KontorX_Navigation_Recursive($rowset);
		$navigation->accept(new Promotor_Navigation_Recursive_Visitor_ShopCategory());

		return $navigation->create();
	}
	
	/**
	 * @param integer $pkNode
	 * @param integer $pkParent
	 * @return void
	 */
	public function move($pkNode, $pkParent = null) {
        $row = $this->findById($pkNode, false);
        if (!$row instanceof KontorX_Db_Table_Tree_Row_Interface) {
            $this->_setStatus(self::FAILURE);
			$this->_addMessage('Record do not exsists or is not instance of "KontorX_Db_Table_Tree_Row_Interface"');
            return;
        }

        $row->setRoot(true);
        if (is_numeric($pkParent)) {
        	$parentRow = $this->findById($pkParent, false);
        	if ($parentRow instanceof KontorX_Db_Table_Tree_Row_Interface) {
        		$row->setParentRow($parentRow);
        		$row->setRoot(false);
        	} else {
        		$this->_addMessage(sprintf('Parent row is null for id "%d"', $pkParent));
        	}
        }

        try {
            $row->save();
            $this->_setStatus(self::SUCCESS);
        } catch (Zend_Db_Table_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
        }
	}
	
	/**
	 * @param integer $id
	 * @param bool $asArray
	 * @return array|KontorX_Db_Table_Tree_Row_Abstract
	 */
	public function findById($id, $asArray = true) {
		$table = $this->getDbTable();
		try {
			$rowset = $table->find((int) $id);
			if(!count($rowset)) {
				$this->_setStatus(self::FAILURE);
				return;
			}

			$this->_setStatus(self::SUCCESS);

			$current = $rowset->current();
			return $asArray
				? $current->toArray()
				: $current;
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addMessage($e->getMessage());
		}
	}
	
	/**
	 * @param string $alias
	 * @param bool $asArray
	 * @return array|KontorX_Db_Table_Tree_Row_Abstract
	 */
	public function findByAlias($alias, $asArray = true) {
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('alias = ?', $alias);

		try {
			$row = $table->fetchRow($where);
			if(!$row instanceof Zend_Db_Table_Row_Abstract) {
				$this->_setStatus(self::FAILURE);
				return;
			}
			$this->_setStatus(self::SUCCESS);
			return $asArray
				? $row->toArray()
				: $row;
		} catch (Zend_Db_Table_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}

	/**
	 * @param int|string $identification
	 * @param bool $asArray
	 * @return array|KontorX_Db_Table_Tree_Row_Abstract|null
	 */
	public function findOne($identification, $asArray = true) {
		if (is_numeric($identification)) {
			return $this->findById($identification, $asArray);
		} else
		if (is_string($identification)) {
			return $this->findByAlias($identification, $asArray);
		}

		return null;
	}

	/**
	 * @param Zend_Db_Select $select
	 * @param array $asArray
	 * @return void
	 */
	public function findAll(Zend_Db_Select $select = null, $asArray = true) {

		$table = $this->getDbTable();
		if (null === $select) {
			$select = $table->select();
		}

		$select
			->where('publicated = ?', '1')
			->order('idx ASC');

		try {
			$rowset = $table->fetchAll($select);
			
			$this->_setStatus(self::SUCCESS);
			
			return $asArray
				? $rowset->toArray() 
				: $rowset;

		} catch(Zend_Db_Exception $e) {
			$this->_setStatus(self::FAILURE);
			$this->_addException($e);
		}
	}
}